//
//  LGECGDetailChartView.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/5/12.
//

#import "LGECGDetailChartView.h"

//#define  itemW  ((Screen_W-viewPix(32))/75.0)

@interface LGECGDetailChartView()
/**   */
@property (nonatomic , strong)UIScrollView *scrollView;

@end

@implementation LGECGDetailChartView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.segment = 1;
        self.backgroundColor = [UIColor whiteColor];
        self.itemWidth = ((Screen_W-viewPix(32))/75.0);
    }
    return self;
}
-(void)setSegment:(NSInteger)segment{
    _segment = segment;
}

-(void)setDataArray:(NSArray *)dataArray{
    _dataArray = dataArray;
    if(dataArray.count == 0) return;
    NSDictionary *valueDic = [self getMinAndMaxNumber:dataArray];
    CGFloat min = [valueDic[@"min"] floatValue];
    CGFloat height = [valueDic[@"max"] floatValue] - [valueDic[@"min"] floatValue];
    
    NSMutableArray *pointArray = [NSMutableArray array];
    for (NSInteger i=0; i<dataArray.count; i++) {
        CGFloat value = [dataArray[i] floatValue];
        NSValue *point = [NSValue valueWithCGPoint:CGPointMake((self.itemWidth/5.0*self.segment)*i, (1-(value-min)/height)*self.bounds.size.height)];
        [pointArray addObject:point];
    }
    
    CGPoint startPoint = [[pointArray objectAtIndex:0] CGPointValue];
    CGPoint lastPoint;
    UIBezierPath *lineBeizer = [UIBezierPath bezierPath];
    [lineBeizer setLineWidth:1.5];
    [lineBeizer moveToPoint:startPoint];

    for (NSInteger i = 0;i < pointArray.count;i++ ) {
        if (i < pointArray.count-1 ) {
            CGPoint nowPoint = [[pointArray objectAtIndex:i] CGPointValue];
            CGPoint futPoint  = [[pointArray objectAtIndex:i+1] CGPointValue];
            [lineBeizer addLineToPoint:nowPoint];
//            [lineBeizer addCurveToPoint:futPoint controlPoint1:CGPointMake((nowPoint.x+futPoint.x)/2, nowPoint.y) controlPoint2:CGPointMake((nowPoint.x+futPoint.x)/2, futPoint.y)];
            if (i == pointArray.count-2) {
                [lineBeizer moveToPoint:futPoint];//添加连线
                lastPoint = futPoint;
            }
        }
    }

    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.path = lineBeizer.CGPath;
    shapeLayer.fillColor = [UIColor clearColor].CGColor;
    shapeLayer.strokeColor = [UIColor colorWithString:@"#3291FF"].CGColor;
    shapeLayer.lineWidth = 1.5;
    [self.layer addSublayer:shapeLayer];
}


-(NSDictionary *)getMinAndMaxNumber:(NSArray *)array{
    CGFloat min = 0;
    CGFloat max = 0;
    for (NSInteger i=0; i<array.count; i++) {
        CGFloat temp = [array[i] floatValue];
        if(min>temp) min = temp;
        if(max<temp) max = temp;
    }
    NSDictionary *tempDic = @{@"max":@(max) , @"min":@(min)};
    return tempDic;
}

- (void)drawRect:(CGRect)rect{
    [super drawRect:rect];
    //画横线
    for (NSInteger i=0; i<=6*5; i++) {
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextSetLineCap(context, kCGLineCapSquare);
        CGContextSetLineWidth(context, 1);
        if(i%5 == 0){
            CGContextSetStrokeColorWithColor(context, [UIColor colorWithString:@"BEDCFF"].CGColor);
        }else{
            CGContextSetStrokeColorWithColor(context, [UIColor colorWithString:@"EFF6FF"].CGColor);
        }
        CGContextBeginPath(context);
        CGContextMoveToPoint(context, 0, self.itemWidth*i);
        CGContextAddLineToPoint(context, rect.size.width, self.itemWidth*i);
        CGContextStrokePath(context);
    }
    
    NSInteger count = rect.size.width/self.itemWidth;
    for (NSInteger i=0; i<count; i++) {
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextSetLineCap(context, kCGLineCapSquare);
        CGContextSetLineWidth(context, 1);
        if(i%5 == 0){
            
            CGContextSetStrokeColorWithColor(context, [UIColor colorWithString:@"BEDCFF"].CGColor);
        }else{
            CGContextSetStrokeColorWithColor(context, [UIColor colorWithString:@"EFF6FF"].CGColor);
        }
        CGContextBeginPath(context);
        CGContextMoveToPoint(context, self.itemWidth*i, 0);
        CGContextAddLineToPoint(context, self.itemWidth*i, rect.size.height);
        CGContextStrokePath(context);
    }
    
}

@end
